<?php
require_once 'client'.EXT;

class Country extends Client{

	function __construct()
	{
		parent::__construct();
		$this->load->model('country_model');
		$this->data['current']	=	'country';
	}
	
	function index($id = null)
	{
		$row	=	array();
		if($id){
			$row	=	$this->country_model->get_by('id',$id);
		}
		
		$this->data['row_con']	=	$row;
		
		if($_SERVER['REQUEST_METHOD'] == 'POST')
		{
			if($id)
			{
				$this->form_validation->set_rules('name','Nationality name','required');
				if($this->form_validation->run() == TRUE){
					$this->country_model->update($id,array('name' => $this->input->post('name')));
					redirect(base_url().'country/');
				}
			}
			else
			{
				$this->form_validation->set_rules('name','Nationality name','required|callback__check_country');
				if($this->form_validation->run() == TRUE){
					$this->country_model->insert(array('name' => $this->input->post('name')));
					redirect(base_url().'country/');
				}
			}
		}
		$this->data['country']	=	$this->country_model->get_all();
		$this->data['tpl_file']		=	'country/index';
		$this->load->view('layout/default',$this->data);
	}
	
	function add()
	{
	
	}
	
	function del($id = null)
	{
		if($id){
			$this->country_model->delete($id);
			redirect(base_url().'country/');
		}
	}
	
	function _check_country($name = null)
	{
		if($this->country_model->get_by(array('name' => $name)))
		{
			$this->form_validation->set_message('_check_country','The %s is existed!');
			return FALSE;
		}
		return TRUE;
	}
	
}